//
//  FKIndicatorView.m
//  FKVideoPlayerDemo
//
//  Created by 云眼科技 on 16/7/28.
//  Copyright © 2016年 CloudEyes. All rights reserved.
//

#import "FKIndicatorView.h"



@interface FKIndicatorView ()
@property (nonatomic, strong) UIImageView *imageView;
@property (nonatomic, strong) UILabel *titleLabel;
@end

@implementation FKIndicatorView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor redColor];
        _dimissTimeInterval = 0.5;
        _dismissAniation = YES;
        
        _imageView = [[UIImageView alloc] init];
        _imageView.contentMode = UIViewContentModeScaleAspectFit;
        [self addSubview:_imageView];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor = [UIColor whiteColor];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:_titleLabel];
    }
    return self;
}

-(void)layoutSubviews{
    [super layoutSubviews];
    
    CGFloat imageView_d = (self.frame.size.width/2) > (self.frame.size.height/2) ? (self.frame.size.height/2) : (self.frame.size.width/2);
    
    self.imageView.frame = CGRectMake(self.bounds.origin.x, self.bounds.origin.y, imageView_d, imageView_d);
    self.imageView.center = CGPointMake(self.frame.size.width/2, self.frame.size.height*3/4/2);
    
    CGFloat maxImageViewY = CGRectGetMaxY(self.imageView.frame);
    self.titleLabel.frame = CGRectMake(self.bounds.origin.x, maxImageViewY, self.frame.size.width, self.frame.size.height-maxImageViewY);
}


-(void)showAnimated:(BOOL)animated onView:(UIView *)view{
    self.center = view.center;
    if (animated) {
        self.alpha = 0.1;   
        [view addSubview:self];
        [UIView animateWithDuration:1 animations:^{
            self.alpha += 1;
        } completion:^(BOOL finished) {
            self.alpha = 1;
        }];
    }else{
        [view addSubview:self];
    }
}


-(void)dismissViewAnimated:(BOOL)animated{
    if (animated) {
        [UIView animateWithDuration:0.3 animations:^{
            self.alpha = 0.5;
        } completion:^(BOOL finished) {
            self.alpha = 1;
            [self removeFromSuperview];
        }];
    }else{
        [self removeFromSuperview];
    }
}


#pragma mark - setters
-(void)setTitle:(NSString *)title{
    _title = title;
    self.titleLabel.text = title;
}


-(void)setImageName:(NSString *)imageName{
    _imageName = imageName;
    self.imageView.image = [UIImage imageNamed:imageName];
}




@end
